Component({
    properties: {},
    data: {},
    observers: {
        'list.**': function (data) {
            const listKey = Object.keys(data)
            const list = {}
            listKey.forEach(item => {
                list[item] = this.data.list[item]
                    .filter(tag => tag.value)
                    .map(tag => tag.name)
            })
            const {
                wash,
                equip,
                base,
                other,
            } = list
            this.setData({
                listLength: wash.length + equip.length + base.length + other.length
            })
        }
    },
    methods: {
        onReady() {
            const eventChannel = this.getOpenerEventChannel()
            eventChannel.on('getHouseEquipment', (
                data = {
                    wash: [],
                    equip: [],
                    base: [],
                    other: [],
                },
                list
            ) => {
                const listKey = Object.keys(list)
                const newList = JSON.parse(JSON.stringify(list))
                listKey.forEach(key => {
                    newList[key] = newList[key].map(item => {
                        return {
                            name: item,
                            value: data[key].indexOf(item) > -1
                        }
                    })
                })
                this.setData({
                    list: newList
                })
            })
        },
        change(e) {
            if (e.target.dataset.type) {
                wx.vibrateShort()
                const { index, type, value } = e.target.dataset
                console.log(index, type, value)
                this.setData({
                    ['list.' + type + '[' + index + '].value']: !value
                })
            }
        },
        submit() {
            const eventChannel = this.getOpenerEventChannel()
            const listKey = Object.keys(this.data.list)
            const list = {}
            listKey.forEach(item => {
                list[item] = this.data.list[item]
                    .filter(tag => tag.value)
                    .map(tag => tag.name)
            })
            eventChannel.emit('setHouseEquipment', list)
            wx.navigateBack()
        }
    }
});
